package com.rocsea.robot.template.impl;

import com.rocsea.robot.config.build.TemplateInfo;
import com.rocsea.robot.factory.KettleTemplateFactory;
import com.rocsea.robot.model.entity.StepAttributeDO;
import com.rocsea.robot.model.entity.TransformationDO;
import com.rocsea.robot.model.enums.KettleBuildStrategyEnum;
import com.rocsea.robot.utils.BeanCopyUtils;
import com.rocsea.robot.utils.KettleBuildUtils;
import com.rocsea.robot.utils.MysqlBuilderUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * 简单的同步移除update_time_id
 * @Author RocSea
 * @Date 2022/12/7
 */
@Component
public class SimpleRemoveUpdateTimeIdTemplate extends SimpleTemplate {
    @Override
    public TemplateInfo getTemplateInfo() {
        return super.getTemplateConfig().getSimpleRemoveUpdateTimeId();
    }

    @Override
    public String processTransformation(TransformationDO transformationDO) {
        TransformationDO buildDO = BeanCopyUtils.copyProperties(transformationDO, TransformationDO.class);
        buildDO.setIdTransformation(KettleBuildUtils.getIdMaxTransRelated().addAndGetIdTransformationMax());
        buildDO.setIdDirectory(KettleBuildUtils.getIdDirectory());
        buildDO.setName("sync_" + KettleBuildUtils.getSourceTableName());
        return MysqlBuilderUtils.buildTransformation(buildDO);
    }

    @Override
    public String processTableInput(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        return super.processTableInputWithRowMeta(stepAttributeDOS);
    }

    @Override
    public String processSqlReplace(String sql) {
        String replace = sql.replace(getTemplateInfo().getName(), KettleBuildUtils.getSourceTableName());
        return replace.substring(replace.indexOf("FROM"));
    }

    @Override
    public String processQueryFromTableName(List<StepAttributeDO> stepAttributeDOS) throws IOException {
        return super.processTableInputWithRowMeta(stepAttributeDOS);
    }

    @Override
    public String processKettleTaskConfig() throws ParseException {
        return super.kettleTaskConfigService.buildKettleTaskConfig(true);
    }

    @Override
    public void afterPropertiesSet() {
        KettleTemplateFactory.register(KettleBuildStrategyEnum.SIMPLE_REMOVE_UPDATE_TIME_ID.getText(), this);
    }

}
